package com.songyang.blog.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songyang.blog.common.lang.Result;
import com.songyang.blog.entity.Post;
import com.songyang.blog.entity.User;
import com.songyang.blog.mapper.PostMapper;
import com.songyang.blog.mapper.UserMapper;
import com.songyang.blog.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 宋阳
 * @since 2021-05-07
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    PostMapper postMapper;

    @Override
    public Result register(User user) {
        int count = this.count(new QueryWrapper<User>()
                .eq("email", user.getEmail())
                .or()
                .eq("username", user.getUsername())
        );

        if (count > 0) return Result.fail("用户名或者邮箱已经被占用了!");
        User userTemp = new User();//创建一个user对象的目的是防止其他的参数也传到数据库当中，比如积分等等
        //这些参数不允许写入到数据库，所以新建一个user对象，只放我们要放入数据库当中的username，password和email数据
        userTemp.setUsername(user.getUsername());
        userTemp.setPassword(SecureUtil.md5(user.getPassword()));//密码加密
        userTemp.setEmail(user.getEmail());
        userTemp.setCreated(new Date());//设置当前的创建时间
        userTemp.setPoint(0);//设置默认积分为0
        userTemp.setVipLevel(0);//设置VIP的级别
        userTemp.setAvatar("/res/images/avatar/1.jpg");//设置默认头像
        userTemp.setCommentCount(0);//设置评论数为0
        userTemp.setPostCount(0);//设置博客数量0
        userTemp.setGender("男");
        userTemp.setIdentity("作者");
        userTemp.setSign("这个人好懒，什么都没留下！");
        userMapper.insert(userTemp);
        return Result.success("注册成功!");
    }

    @Override
    public User login(String email, String password) {
        User user = this.getOne(new QueryWrapper<User>().eq("email", email));

        if (user == null) {
            throw new UnknownAccountException();
        } else if (!user.getPassword().equals(password)) {
            throw new IncorrectCredentialsException();
        }
        user.setLasted(new Date());
        this.updateById(user);
        User profile = new User();
        if (password != null && password.equals(user.getPassword()))
            log.info("在userService中认证数据库与获得的一致");
        else log.info("在userService中认证数据库与获得的不一致");
        BeanUtils.copyProperties(user, profile);
        return profile;
    }

    @Override
    public List<Post> getPosts(Long profileId) {
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", profileId)
                .orderByDesc("created");
        IPage<Post> page = new Page<>(1, 10);
        IPage<Post> userIPage = postMapper.selectPage(page, queryWrapper);
        return userIPage.getRecords();
    }


}



















